package com.example.tys.baseapplication.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.tys.baseapplication.R;

/**
 * 一圈 一圈的向外扩散，中间有个TexView，点击开始/停止动画
 *
 * @author tys
 * @date 2019/4/12
 */

public class Ripple extends FrameLayout {

    private TextView mTextView;
    private Paint mPaint;
    private int mHeight;
    private int mWidth;
    private int mCount;
    private int div = 30;
    private int mSelected = 1;
    private boolean mIsRunning;

    public Ripple(@NonNull Context context) {
        super(context);
    }


    public Ripple(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStrokeWidth(3);
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
        div = dp2px(div);
        LayoutParams layoutParams = new LayoutParams(2 * div, 2 * div, Gravity.CENTER);
        mTextView = new TextView(getContext());
        mTextView.setText("发送");
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setBackgroundResource(R.drawable.sweep);
        mTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIsRunning) {
                    mTextView.setText("发送");
                    cancel();
                } else {
                    mTextView.setText("停止");
                    start();
                }
            }
        });
        addView(mTextView, layoutParams);
    }

    private int dp2px(int div) {
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, div, getResources().getDisplayMetrics()));
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        for (int i = 1; i <= mSelected; i++) {
            canvas.drawCircle(mWidth / 2, mHeight / 2, i * div, mPaint);
            mPaint.setAlpha(255 - i * (255 / mCount));
        }
        mSelected = ++mSelected > mCount ? 1 : mSelected;
        if (mIsRunning) {
            postInvalidateDelayed(350);
        } else {
            mSelected = 1;
            canvas.drawColor(Color.WHITE);
        }
        super.dispatchDraw(canvas);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHeight = h;
        mWidth = w;
        int mRadios = Math.min(mWidth / 2, mHeight / 2);
        mCount = mRadios / div;
    }

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.isRunning = mIsRunning;
        return ss;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        mIsRunning = ss.isRunning;
        if (mIsRunning) {
            mTextView.setText("停止");
        } else {
            mTextView.setText("发送");
        }
        requestLayout();
    }

    public void start() {
        if (mIsRunning) {
            return;
        }
        mIsRunning = true;
        invalidate();
    }

    public void cancel() {
        mIsRunning = false;
    }


    static class SavedState extends BaseSavedState {
        boolean isRunning;

        SavedState(Parcelable superState) {
            super(superState);
        }

        SavedState(Parcel in) {
            super(in);
            isRunning = (Boolean) in.readValue(null);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeValue(isRunning);
        }

        @Override
        public String toString() {
            return "SavedState{" +
                    "isRunning=" + isRunning +
                    '}';
        }

        public static final Parcelable.Creator<SavedState> CREATOR =
                new Parcelable.Creator<SavedState>() {
                    @Override
                    public SavedState createFromParcel(Parcel in) {
                        return new SavedState(in);
                    }

                    @Override
                    public SavedState[] newArray(int size) {
                        return new SavedState[size];
                    }
                };
    }

}
